package edu.gdkm.demo10.dasai;

import java.util.Random;

public class QuThread implements Runnable{
    private Integer[] money;

    public QuThread(Integer[] money) {
        this.money = money;
    }

    @Override
    public void run() {
        while (true){
            synchronized(money) {
                this.apply();
            }
            if(money[0]==0){
                System.out.println("余额不足，请尽快赚钱");
            }
            try {
                Thread.sleep(new Random().nextInt(1000)+1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized void apply(){
        if(money[0]>0){
            int applymoney = new Random().nextInt(300)+1 ; // 1-300之间的随机整数
            if(money[0]<applymoney){
                applymoney = money[0];
            }
            money[0] = money[0] -applymoney;
            System.out.println(Thread.currentThread().getName()
                    +"取出￥"+applymoney+"购买物质,当前余额："+money[0]);
        }

    }
}
